import {
  fetchHome
} from '../../services/home/home';
import {
  fetchGoodsList
} from '../../services/good/fetchGoods';
import Toast from 'tdesign-miniprogram/toast/index';
// import drawQrcode from '../../utils/weapp.qrcode.esm.js'
import {
  getSwiper,
  getWorklist
} from '../../api/index'


Page({
  data: {
    imgSrcs: [],
    imgSrcsA: [], //首页轮播图
    imgMid: [], //首页中部图
    imgCostom: [], //精彩客片
    imgAll: [], //所有图片
    tabList: [],
    goodsList: [],
    goodsListLoadStatus: 0,
    pageLoading: false,
    current: 1,
    autoplay: true,
    duration: '500',
    interval: 5000,
    codeUrl: '',
    navigation: {
      type: 'dots'
    },
    swiperImageProps: {
      mode: 'scaleToFill'
    },
    workList: [],
    baseUrl: 'https://oss.peibanmao.com/',
  },



  goodListPagination: {
    index: 0,
    num: 20,
  },

  privateData: {
    tabIndex: 0,
  },

  onShow() {
    this.getTabBar().init();
  },

  onLoad() {
    this.init();

  },
  //转发给朋友
  onShareAppMessage() {
    const promise = new Promise(resolve => {
      setTimeout(() => {
        resolve({
          title: '时忆旅拍'
        })
      }, 2000)
    })
    return {
      title: '时忆旅拍',
      path: '/pages/home/home',
      promise
    }
  },
  //分享到朋友圈
  onShareTimeline() {},
  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },

  onPullDownRefresh() {
    this.init();

  },

  init() {
    this.loadHomePage();
    // this.getCode();
    this.getSwiperFn()
    this.getWorklistFn()

  },

  // getCode() {
  //   const query = wx.createSelectorQuery()
  //   query.select('#myQrcode')
  //     .fields({
  //       node: true,
  //       size: true
  //     })
  //     .exec((res) => {
  //       var canvas = res[0] && res[0].node

  //       // 调用方法drawQrcode生成二维码
  //       drawQrcode({
  //         canvas: canvas,
  //         canvasId: 'myQrcode',
  //         width: 260,
  //         padding: 30,
  //         background: '#ffffff',
  //         foreground: '#000000',
  //         text: '测试',
  //       })

  //       // 获取临时路径
  //       wx.canvasToTempFilePath({
  //         canvasId: 'myQrcode',
  //         canvas: canvas,
  //         x: 0,
  //         y: 0,
  //         width: 80,
  //         height: 80,
  //         destWidth: 80,
  //         destHeight: 80,
  //         success: (res) => {
  //           console.log('二维码临时路径：', res.tempFilePath)
  //           this.setData({
  //             codeUrl: res.tempFilePath
  //           });
  //         },
  //         fail(res) {
  //           console.error(res)
  //         }
  //       })
  //     })
  // },


  getSwiperFn() {
    getSwiper().then((res) => {
      console.log(res.data.item, 'res');
      const arr = res.data.item.filter(v => v.title === "首页轮播");
      const arrB = res.data.item.filter(v => v.title === "首页中部图片");
      // const arrC = res.data.item.filter(v => v.title === "首页洪崖洞街拍图片");
      const urls = arr.map(v => "https://oss.peibanmao.com/" + v.url);
      const imgMidArr = arrB.map(v => "https://oss.peibanmao.com/" + v.url);
      // const imgCostomArr = arrC.map(v => "https://oss.peibanmao.com/" + v.url);
      // console.log(imgCostomArr)
      this.setData({
        imgSrcsA: urls,

      });
      this.setData({
        imgMid: imgMidArr,
      });
      // this.setData({
      //   imgCostom: imgCostomArr,
      // });
    })
  },
  getWorklistFn() {
    getWorklist({
      pageSize: 10,
      pageNum: 1,
    }).then(res => {
      this.setData({
        workList: res.data.item
      })
      console.log(res, 'res')
    })
  },
  goToInfo(e) {
    const id = e.currentTarget.dataset.item.id
    wx.navigateTo({
      url: '/pages/goods/itemWork/index?id=' + id,
    })
  },
  handleContact(e) {
    console.log(e.detail.path, 'e.detail.path')
  },
  loadHomePage() {
    wx.stopPullDownRefresh();

    this.setData({
      pageLoading: true,
    });
    fetchHome().then(({
      swiper,
      tabList
    }) => {
      this.setData({
        tabList,
        imgSrcs: swiper,
        pageLoading: false,
      });
      this.loadGoodsList(true);
    });
  },

  tabChangeHandle(e) {
    this.privateData.tabIndex = e.detail;
    this.loadGoodsList(true);
  },

  onReTry() {
    this.loadGoodsList();
  },
  goMaster() {
    console.log('jieshuo')
    wx.switchTab({
      url: '/pages/goods/category/index',
    })
  },

  async loadGoodsList(fresh = false) {
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
    }

    this.setData({
      goodsListLoadStatus: 1
    });

    const pageSize = this.goodListPagination.num;
    let pageIndex = this.privateData.tabIndex * pageSize + this.goodListPagination.index + 1;
    if (fresh) {
      pageIndex = 0;
    }

    try {
      const nextList = await fetchGoodsList(pageIndex, pageSize);
      this.setData({
        goodsList: fresh ? nextList : this.data.goodsList.concat(nextList),
        goodsListLoadStatus: 0,
      });

      this.goodListPagination.index = pageIndex;
      this.goodListPagination.num = pageSize;
    } catch (err) {
      this.setData({
        goodsListLoadStatus: 3
      });
    }
  },


  navToSearchPage() {
    wx.navigateTo({
      url: '/pages/goods/search/index'
    });
  },

});